/*=tree==============*/
var setting = {
    check: {
        enable: true,
        autoCheckTrigger: true,
        chkboxType: { "Y": "ps", "N": "ps" }
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};
var treeObj;


$(function () {
    PL.grid("roleGrid", {
        colModel: [
            {header: '角色名', name: 'roleDes', align: 'center', width: 150  },
            {header: '创建日期', name: 'createDate', align: 'center', width: 80  },
            {header: '操作', name: 'operation', width: 'autoExpand', align: 'center',
                renderer: function (colValue, rowData, rowIndex) {
                    var id = rowData.id;
                    var flag = rowData.flag;
                    var div = "<div>";
                    div = div + "<span><button type='button' class='btn btn-mini btn-info' onclick=Rupdate('" + id + "')>修改</button></span>&nbsp;&nbsp;";
                    div = div + "<span><button type='button' class='btn btn-mini btn-danger' onclick=Rdelete('" + id + "')>删除</button></span>&nbsp;&nbsp;";
                    div = div + "<span><button type='button' class='btn btn-mini' onclick=RshowTree('" + id + "')>查看角色功能</button></span>";
                    //由于点击按钮会触发onRowSelect，因此用一个按钮替代RowSelect
                    div = div + "</div>";
                    return div;
                }}
        ],
        //由于点击修改,删除按钮；会并发执行onRowSelect,因此采用按钮方式
        //onRowSelect: function (rowIndex, rowData, event) {},
        title: '用户列表'
    });
    search();

    /*====================================*/
    $("#btnSearch").click(function () {
        search();
    });
    $("#search").bind('keydown', function (e) {
        var key = e.which;
        if (key == 13) {
            search();
        }
    });

    /*============新增dialog=========*/
    $("#recordDialog").omDialog({
        autoOpen: false,
        modal: true,
        width: 600,
        height: 400
    });

    $("#btnAdd").click(function () {
        $("#recordForm").find(".uyunsky_base").val("");
        $("#recordForm")[0].reset();
        $("#recordDialog").omDialog('open');
    });
    $("#btnSave").click(function () {
        var data = PL.getData("recordForm");
        var id = data.id;
        if (PL.isEmpty(id)) {
            data.optype = "ADD";
        } else {
            data.optype = "UPD";
        }

        PL.doService("PLRoleService", data, {
            success: function () {
                $("#recordDialog").omDialog('close');
                $("#roleGrid").omGrid("reload");
            }
        });
    });


    /*================角色树相关====================*/
    $("#updateTran").click(function () {
        var nodes = treeObj.getCheckedNodes();
        var trans = [];
        for (var i = 0; i < nodes.length; i++) {
            trans.push(nodes[i].id);
        }

        var roleid = $('#roleNameSpan').attr("roleid");

        PL.doService("PLTranService", {
            optype: 'UPD',
            roleid: roleid,
            trans: trans
        }, {
            success: function (data) {
                alert("更新成功!");
            }
        });
    });
});

function search() {
    var data = {
        optype: 'SEARCH',
        parameter: $("#search").val()
    };
    $("#roleGrid").omGrid('load', 'PLRoleService', data);
}

function Rupdate(id) {
    PL.doService("PLRoleService", {
        optype: 'GET',
        id: id
    }, {
        success: function (data) {
            $("#recordDialog").omDialog('open');
        }
    });
}

function Rdelete(id) {
    var rowData = getRowDataById(id);
    if (confirm("确认删除[" + rowData.roleDes + "]")) {
        PL.doService("PLRoleService", {
            optype: 'DEL',
            id: rowData.id
        }, {
            success: function () {
                $("#roleGrid").omGrid('reload');
            }
        });
    }
}

function RshowTree(id) {
    PL.doService("PLTranService", {roleid: id, optype: 'GET'}, {
        success: function (data) {
            var rowData = getRowDataById(id);
            var roleDes = rowData.roleDes;
            var treeNodes = data.menu;
            if (PL.isArray(treeNodes)) {
                $('#roleNameSpan').attr("roleid", id).text(roleDes);

                treeObj = $.fn.zTree.init($("#roleTree"), setting, treeNodes);
                var flag = rowData.flag;
                if (flag == '1') {
                    treeObj.checkAllNodes(true);
                }
            } else {
                alert("获取角色[" + roleDes + "]对应功能树失败");
            }
        }
    })
}

function getRowDataById(id) {
    var rowDatas = $("#roleGrid").omGrid('getData').rows;
    for (var i = 0; i < rowDatas.length; i++) {
        var r = rowDatas[i];
        if (id == r.id) {
            return r;
        }
    }
}